package com.itheima.core.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.core.dao.product.ColorDao;
import com.itheima.core.dao.product.ProductDao;
import com.itheima.core.dao.product.SkuDao;
import com.itheima.core.pojo.product.Product;
import com.itheima.core.pojo.product.Sku;
import com.itheima.core.pojo.product.SkuQuery;

/**
 * @author zhang.cheng
 *
 *         2017年3月15日
 */
@Service("cmsService")
@Transactional
public class CmsServiceImpl implements CmsService {
	
	@Autowired
	private ProductDao productDao;
	
	@Autowired 
	private SkuDao skuDao;
	
	@Autowired 
	private ColorDao colorDao;

	@Override
	public Product toProductDetail(Long id) {
		Product product = productDao.selectByPrimaryKey(id);
		return product;
	}

	@Override
	public List<Sku> querySkuList(Long id) {
		SkuQuery skuQuery = new SkuQuery();
		//通过商品id查询库存，并且查询条件为库存大于零
		skuQuery.createCriteria().andProductIdEqualTo(id).andStockGreaterThan(0);
		List<Sku> skus = skuDao.selectByExample(skuQuery);
		for (Sku sku : skus) {
			sku.setColor(colorDao.selectByPrimaryKey(sku.getColorId()));
		}
		return skus;
	}

}
